package com.app.manage.utils;

import cn.hutool.extra.ssh.JschUtil;
import cn.hutool.extra.ssh.SshjSftp;
import com.app.manage.moudles.server.entity.TServer;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.Charset;

public class SshUtils {


    public static Boolean checkSshConnect(String ip,Integer port,String user,String pwd,Integer timeOutSeds) {
        if(timeOutSeds == null){
            timeOutSeds = 10;
        }
        Session session = null;
        try {
            session =  JschUtil.openSession(ip,port,user,pwd,1000*timeOutSeds);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            JschUtil.close(session);
        }
        return true;
    }



//    public static void main(String[] args) {
//        String cmd = "cd /home/projects/myTest/ && nohup java -jar my-test-0.0.1-SNAPSHOT.jar >>my.log 2>&1 &";
////        String cmd = "java -version";
//        execForStr("192.168.17.144",22,"root","root",cmd);
//
//
//
//        System.err.println("-------------");
//
//
//
////
//    }




    public static String execForStr(String ip,Integer port,String user,String pwd,String cmd){

        JSch jsch = new JSch();
        Session session = null;
        ChannelExec channel = null;
        try {
            session = jsch.getSession(user, ip, port);
            session.setPassword(pwd);
            session.setConfig("StrictHostKeyChecking", "no"); // 不验证HostKey，生产环境请谨慎使用
            session.setTimeout(10000); // 设置超时时间
            session.connect();
            channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand(cmd); // 设置你的命令
            channel.setErrStream(System.err); // 错误输出到标准错误流
            channel.connect(); // 连接并执行命令
            // 处理输出等...
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }

        }
        return "成功";
    }

    public static String execForShell(String ip,Integer port,String user,String pwd,String cmd){

        JSch jsch = new JSch();
        Session session = null;
        ChannelExec channel = null;
        try {
            session = jsch.getSession(user, ip, port);
            session.setPassword(pwd);
            session.setConfig("StrictHostKeyChecking", "no"); // 不验证HostKey，生产环境请谨慎使用
            session.setTimeout(10000); // 设置超时时间
            session.connect();
            channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand("bash " +cmd); // 设置你的命令
            channel.setErrStream(System.err); // 错误输出到标准错误流
            channel.connect(); // 连接并执行命令
            // 处理输出等...
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }

        }
        return "成功";
    }

    public static  String killJarCmd(String ip,Integer port,String user,String pwd,String jarName){

        String pidCmd = "ps aux |grep "+jarName +  " | grep -v grep | awk '{print $2}'";

        Session session = null;
        try {
            //获取到进程id
            session =  JschUtil.createSession(ip,port,user,pwd);
            String pid =  JschUtil.exec(session,pidCmd,Charset.forName("utf-8"));
            if(StringUtils.isNotEmpty(pid)){
                pid = pid.trim();
                String killCmd = "kill -9  "+pid;
                String res = JschUtil.exec(session,killCmd,Charset.forName("utf-8"));
            }
            //获取到
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(session != null){
                session.disconnect();
            }
        }






        return null;
    }

    public static void main(String[] args) {
        String res = killJarCmd("192.168.0.171",22,"root","123qwe!@#QWE","app-manage-0.0.1-SNAPSHOT.jar");
    }


}
